#!/usr/bin/python
# -*-coding:utf-8-*-
import os
import pandas as pd
import warnings
# warnings.filterwarnings('once')
# warnings.filterwarnings('ignore')

from factor_test.factor_schedule_test import multi_process_by_signal_v2 as factor_test_main

### 底层读取数据的依赖（不提供）
from zbc_factor_lib.base.factors_library_base import NewRQFactorLib as DataReader

# TODO - 公用参数
# 全市场
factor_test_on_whole_market_config = {
    'start_date': '2017-01-01',
    'end_date': '2019-12-31',
    'group_method': 'cap',
    # 'weight_method': 'cir_cap',
    'weight_method': 'ew',
    'rebalance_type': 'startegy',
    'rebalance_periods': 21,
    'universe_pool': 'all',
    'benchmark_index_code': '000905',
    'group_quantile': 10,
    # 'industry_type': 'sw1',
    'industry_type': 'ci1',
    'liquidity_filter': 'money',
    'liquidity_filter_period': 21,
    'factor_database': 'gp_factors',
    'summary_result_save_dir': './gp_factors',
}

# 中证500股票池
factor_test_on_csi500_pool_config = {
    'start_date': '2017-01-01',
    'end_date': '2019-12-31',
    'group_method': 'cap',
    # 'weight_method': 'cir_cap',
    'weight_method': 'ew',
    'rebalance_type': 'strategy',
    'rebalance_periods': 21,
    'universe_pool': '000905',
    'benchmark_index_code': '000905',
    'group_quantile': 3,
    'industry_type': 'ci1',
    'liquidity_filter': None,
    'liquidity_filter_period': 21,
    'factor_database': 'gp_factors',
    'summary_result_save_dir': './gp_factors',
}


# 给定基准池
factor_test_on_given_basic_pool_config = {
    'start_date': '2017-01-01',
    'end_date': '2019-12-31',
    'group_method': 'cap',
    # 'weight_method': 'cir_cap',
    'weight_method': 'ew',
    'rebalance_type': 'strategy',
    'rebalance_periods': 5,
    'benchmark_index_code': '000905',
    'group_quantile': 3,
    'industry_type': 'ci1',
    'liquidity_filter': None,
    'liquidity_filter_period': 21,
    'factor_database': 'gp_factors',
    'summary_result_save_dir': './gp_factors/2017_2019/gp_factors',
}

if __name__ == '__main__':
    excluded_factor_list = [
        'scale_circulate_market_size',
        'scale_total_market_size',
    ]

    # factor_test_config = factor_test_on_whole_market_config
    factor_test_config = factor_test_on_csi500_pool_config

    db = 'gp_factors'
    # db = 'gp_factors/barra_stripping'

    # TODO - CHANGE
    batch = 40

    start_id = 0

    save_sub_dir = '20200416/%s' % db

    factor_test_config.update({#'universe_pool':basic_pool,
                               'group_quantile': 5,
                               # 'group_quantile': 10,
                               'summary_result_save_dir':'./gp_factors/%s' % (save_sub_dir),
                               'rebalance_type': 'fixed',
                               # 'rebalance_type': 'strategy',
                               # 'rebalance_periods': 3,
                               'rebalance_periods': 10,
                               'weight_method': 'cir_cap',
                               'start_date': '2017-01-01',
                               'end_date': '2020-03-31',
                               'factor_database':db})

    data_reader = DataReader(db=db)

    ths_factor_name_list = data_reader.show_factor_library_db()
    ths_factor_name_list = [fn.split('.')[0] for fn in ths_factor_name_list if 'h5' in fn]

    ths_factor_name_list = list(set(ths_factor_name_list).difference(excluded_factor_list))

    # ths_factor_name_list = [fn for fn in ths_factor_name_list if fn.split('_')[0] in ['event']]
    print('total analysis factor is', len(ths_factor_name_list))

    # TODO - start to new_test
    # TODO - by time
    factor_test_main(factor_name_list=ths_factor_name_list,
                     batch=batch,
                     start_id=start_id,
                     **factor_test_config)

